<template>
  <div class="box">
    <mt-spinner v-show="loading" style="margin: 0 auto;" color="#e72e0d" type="double-bounce"></mt-spinner>
    <div v-if="step === '1'">
      <div class="tip">
        <img src="../../assets/tip.png"> 为保证您的帐号安全，更换邮箱前需要进行安全验证
      </div>
      <div class="border">
        <label class="text">邮箱</label>
        <input type="text" v-model.trim="email" placeholder="" disabled>
      </div>
      <div class="border">
        <input type="text" v-model.trim="code" placeholder="请输入收到的邮箱验证码" maxlength="6">
        <label class="send sending" v-if="sending">{{sending}}s</label>
        <label class="send" @click="sendCode('old')" v-else>发送验证码</label>
      </div>
      <div class="btn">
        <mt-button class="f14" type="primary" v-if="!loading" @click.native="nextStep()">
          <span>下一步</span>
        </mt-button>
        <mt-button class="f14" type="default" disabled v-else>
          <mt-spinner color="#333" :type="3"></mt-spinner>
        </mt-button>
      </div>
    </div>
    <div v-if="step === '2'">
      <div class="tip">
        <img src="../../assets/tip.png"> 为保证您的帐号安全，更换邮箱前需要进行安全验证
      </div>
      <div class="border">
        <label class="text">新邮箱</label>
        <input type="text" v-model.trim="newEmail" placeholder="">
      </div>
      <div class="border">
        <input type="text" v-model.trim="newCode" placeholder="请输入收到的邮箱验证码" maxlength="6">
        <label class="send sending" v-if="sendingNew">{{sendingNew}}s</label>
        <label class="send" @click="sendCode('new')" v-else>发送验证码</label>
      </div>
      <div class="btn">
        <mt-button class="f14" type="primary" v-if="!loading" @click="submit">
          <span>提交</span>
        </mt-button>
        <mt-button class="f14" type="default" disabled v-else>
          <mt-spinner color="#333" :type="3"></mt-spinner>
        </mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import { Toast } from 'mint-ui'
import * as api from '@/service/api'
export default {
  components: {
  },
  data () {
    const email = this.$store.state.me.user.email
    return {
      email: email,
      newEmail: '',
      code: '',
      newCode: '',
      step: '1',
      sending: 0,
      sendingNew: 0,
      loading: false
    }
  },
  methods: {
    nextStep () {
      if (!/^\d{6}$/.test(this.code)) {
        Toast('请输入6位数字验证码')
        return false
      }
      this.verifyCode({
        code: this.code,
        email: this.email
      }).then(res => {
        if (res) {
          this.step = '2'
        }
      })
    },
    async verifyCode (data) {
      try {
        let $return = false
        this.loading = true
        await api.verifyCode({
          type: 2,
          ...data
        })
        this.loading = false
        $return = true
        return $return
      } catch (e) {
        console.log(e)
        this.loading = false
        return false
      }
    },
    async redeemByEmail () {
      // 邮箱验证码
      try {
        await api.redeemByEmail({
          email: this.email
        })
        return true
      } catch (e) {
        console.log(e)
      }
    },
    sendCode (type) {
      var status = true
      if (!/^([-_.0-9A-Za-z]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/.test(this.email)) {
        Toast('请输入正确的邮箱')
        status = false
      }
      if (!status) {
        return false
      }
      if (type === 'old') {
        this.sending = 60
        let totime = setInterval(() => {
          this.sending--
          if (this.sending < 1) {
            this.sending = 0
            clearInterval(totime)
          }
        }, 1000)
        this.redeemByEmail()
      } else {
        this.sendingNew = 60
        let totime = setInterval(() => {
          this.sendingNew--
          if (this.sendingNew < 1) {
            this.sendingNew = 0
            clearInterval(totime)
          }
        }, 1000)
        this.redeemByNewEmail()
      }
    },
    sendCodeNew () {
      var status = true
      if (!/^([-_.0-9A-Za-z]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/.test(this.email)) {
        Toast('请输入正确的邮箱')
        status = false
      }
      if (!status) {
        return false
      }

      this.sendingNew = 60
      let totime = setInterval(() => {
        this.sendingNew--
        if (this.sendingNew < 1) {
          this.sendingNew = 0
          clearInterval(totime)
        }
      }, 1000)
      this.redeemByNewEmail()
    },
    async redeemByNewEmail () {
      try {
        await api.redeemByNewEmail({
          email: this.newEmail
        })
      } catch (error) {
        console.log(error)
      }
    },
    async submit () {
      var status = true
      if (!/^\d{6}$/.test(this.newCode)) {
        Toast('请输入6位数字验证码')
        status = false
      } else if (!/^([-_.0-9A-Za-z]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/.test(this.newEmail)) {
        Toast('请输入正确的邮箱')
        status = false
      }
      if (!status) {
        return false
      }
      this.loading = true
      try {
        await api.modifyUserEmail({
          code: this.newCode,
          email: this.newEmail
        })
        this.$store.dispatch('getUserInfo')
        Toast('邮箱修改成功')
        this.$router.push('/set/index')
      } catch (error) {
        console.log(error)
      }
      this.loading = false
    }
  }
}
</script>
<style lang="scss" scoped>
.box {
  background: #ffffff;
  padding: 0 30px;
}
input:disabled {
  background-color: #fff;
  color: #ccc;
}
.tip {
  padding-top: 30px;
  font-size: 24px;
  color: #808080;
  display: flex;
  align-items: center;
  img {
    height: 36px;
    margin-right: 15px;
  }
}
.border {
  margin-top: 30px;
  border: 1px solid #e4e4e4;
  border-radius: 9px;
  padding: 20px;
  display: flex;
  align-items: center;
  .text {
    padding-right: 20px;
  }
  .send {
    padding-left: 20px;
    color: #186bd0;
    font-size: 28px;
    white-space: nowrap;
    border-left: 1px #e4e4e4 solid;
    text-align: center;
    &.sending {
      width: 6em;
      color: #ccc;
    }
  }
  input {
    flex: 1;
  }
}
.btn {
  padding: 30px 0;
  .mint-button {
    width: 100%;
    &.mint-button--primary {
      background: #f15d43;
      color: #fff;
    }
    display: flex;
    justify-content: center;
  }
}
</style>
